package com.xy.demo.controller;


import com.xy.demo.Util.ResultUtil;
import com.xy.demo.Util.Util;
import com.xy.demo.model.Bill;
import com.xy.demo.model.Params;
import com.xy.demo.model.ResultInfo;
import com.xy.demo.service.BillService;
import com.xy.demo.service.RoleService;
import com.xy.demo.thread.CommonExecutor;
import com.xy.demo.thread.ThreadForAutoExecuteRun;
import com.xy.demo.thread.ThreadRun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/bill")
@RestController
public class BillController {

    @Autowired
    private BillService billService;

    @RequestMapping("/getAll")
    private ResultInfo getAll(@RequestBody Params params ) throws Exception{
        ResultInfo roles = billService.getAll(params);
        return roles;
    }

    @RequestMapping("/test")
    private ResultInfo test() throws Exception{
        ResultInfo info = new ResultInfo();
        info.setMsg("test");
        return info;
    }

    /**
     * @description: todo 多线程测试需要实现ThreadRun方法
     * @author: yang
     * @date: 2023/8/9 17:11
     * @param:
     * @return:
     **/
    @RequestMapping("/AdapterObjectOne")
    private ResultInfo AdapterObjectOne(){
        long start = System.currentTimeMillis();
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < 2000; i++) {
            list.add(Util.toLong(i));
        }
        if(list.size()>0){
            for (Long change : list) {
                Map map = new HashMap();
                map.put("changeId", change);
                CommonExecutor.getCommonExecutor().submit(new ThreadRun(new ThreadForAutoExecuteRun().getClass().getName(),"insetAutoExecuteForStockStatusChange", map));
            }
        }
        return ResultUtil.getResultInfoOk("执行任务消耗了 ：" + (System.currentTimeMillis() - start) + "毫秒");
    }
    @RequestMapping("/AdapterObjectTwo")
    private ResultInfo AdapterObjectTwo(){
        long start = System.currentTimeMillis();
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < 2000; i++) {
            list.add(Util.toLong(i));
        }
        if(list.size()>0){
            for (Long change : list) {
                Bill bill = new Bill();
                bill.setBillNo(Util.toString(change));
                billService.insertOne(bill);
            }
            return ResultUtil.getResultInfoOk("执行任务消耗了 ：" + (System.currentTimeMillis() - start) + "毫秒");
        }else{
            return ResultUtil.getResultInfoOk("errors");
        }
    }



}
